package com.dituhui.web.saferoute;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.dituhui.tool.gis.Point;
import com.dituhui.web.saferoute.util.MapDataReader;
import com.dituhui.web.saferoute.vo.SafeRoute;
import com.dituhui.web.saferoute.vo.SafeTargetPoint;

import net.sf.json.JSONObject;

public class Weixin extends HttpServlet {
	private static final Logger LOGGER = Logger.getLogger(Weixin.class);

	public void init() throws ServletException {
	}

	// http://gps.dituhui.com/saferoute/weixin?x=104.066541&y=30.572269&communityName=guanghua
	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String accountId = request.getParameter("a");
		String command = request.getParameter("cmd");
		String x = request.getParameter("x");
		String y = request.getParameter("y");
		// meinian
		String communityName = request.getParameter("communityName");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		LOGGER.info("communityName=" + communityName);
		JSONObject jsonObject = new JSONObject();
		// 安全出口
		List<SafeTargetPoint> safeTargetPoints = MapDataReader.getSafeTargetPoints(communityName + "SafeTarget");
		jsonObject.accumulate("safeTargetPoints", safeTargetPoints);
		// 安全出口
		List<SafeTargetPoint> safeEquips = MapDataReader.getSafeTargetPoints(communityName + "SafeEquip");
		jsonObject.accumulate("safeEquips", safeEquips);
		// 测试点
		List<SafeTargetPoint> testPoints = MapDataReader.getSafeTargetPoints(communityName + "TestPoint");
		jsonObject.accumulate("testPoints", testPoints);
		// 安全逃生路线
		List<SafeRoute> safeRoutes = MapDataReader.getSafeRoutes(communityName + "Route");
		try {
			if (x != null && x.length() > 0 && !"0".equals(x)) {
				Point point = new Point(Double.parseDouble(x), Double.parseDouble(y));
				SafeRoute nearestSafeRoute = null;
				double nearestDistance = -1;
				for (SafeRoute safeRoute : safeRoutes) {
					double distance = safeRoute.distanceTo(point);
					if (distance > 300) {
						// 距离大于300米的不进行计算
						continue;
					}
					if (nearestDistance < 0) {
						nearestDistance = distance;
						nearestSafeRoute = safeRoute;
					} else {
						if (distance < nearestDistance) {
							nearestDistance = distance;
							nearestSafeRoute = safeRoute;
						}
					}
				}
				if (nearestSafeRoute != null) {
					nearestSafeRoute.setStatus("nearest");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		jsonObject.accumulate("safeRoutes", safeRoutes);
		// 安全区域
		List<SafeRoute> safeAreas = MapDataReader.getSafeRoutes(communityName + "SafeArea");
		jsonObject.accumulate("safeAreas", safeAreas);

		out.print(jsonObject.toString());

		out.flush();
		out.close();
	}
}
